#include <iostream>
using namespace std;
#include <string.h>
#include <iomanip>
#include <fstream>
int main01(int argc, char const *argv[]){
        char mybuf[1024];
        int     myInt;
        long  myLong;
        
        cin>>myInt;
        
        cin>>myLong;

        cin>>mybuf; //遇见空格停止接收 数据

        cout<<"myInt: "<<myInt<<" myLong"<<myLong<<" mybuf:" <<mybuf<<endl;

        return 0;
}
/*
调用该函数是从缓冲区读走数据 缓冲区的数据因为读走了 缓冲区大小会减少
标准输入流对象
        cin.get() //一次只能读取一个字符
        cin.get(一个参数) //读一个字符
        cin.get(三个参数) //可以读字符串
        cin.getline() 可以接收空格
        cin.ignore() 忽略几个缓冲区的数据
        cin.peek() 读一下缓冲区 看看缓冲区后面是否有数据 如果有数据会返回缓冲区第一个字符
        cin.putback() 从缓冲区读一个字符 然后再吐给缓冲区 返回读的那个字符
*/
int main02(int argc, char const *argv[])
{
        /*
        char ch;
        while ((ch = cin.get()) != EOF ) //阻塞挂起
        {
                cout<<ch<<endl;
        }*/

        /*
        cout<<"cin.get(a) 如果缓冲区没有数据 则程序阻塞 \n";
        char a,b,c;
        cin.get(a);
        cin.get(b);
        cin.get(c);

        cout<<"a = "<<a<<" b = "<<b <<" c = "<<c<< " 因为缓冲区有数据,程序不会阻塞\n";

        cin.get(a).get(b).get(c); //链式编程

        cout<<"a = "<<a<<" b = "<<b <<" c = "<<c<<endl;*/

        char buf1[256];
        char buf2[256];

        cout<<"请输入一个字符串 含有多个空格 aa bb cc  dd\n";

        cin>>buf1;
        cin.getline(buf2, 256);
        
        cout<<"buf1:"<<buf1<<"buf2:"<<buf2<<endl;

        return 0;
}
int main03(int argc, char const *argv[])
{
        char buf1[256];
        char buf2[256];

        cout<<"请输入一个字符串 含有多个空格 aa bbccdd\n";

        cin>>buf1;
        cin.ignore(2);
        int myint = cin.peek(); //偷偷看看缓冲区后面是否有数据
        cout<<"myint:"<<myint<<endl;

        cin.getline(buf2, 256);
        
        cout<<"buf1:"<<buf1<<"buf2:"<<buf2<<endl;
        return 0;
}
/*
标准输出流对象cout
        cout.flush()
        cout.put();
        cout.write(); //二进制流的输入输出
        cout.width();
        cout.fill();
        cout.setf(标记)
*/

int main04(int argc, char const *argv[])
{
        cout<<"hello"<<endl;
        cout.put('h').put('e').put('l');

        char *p = "hello aaazler";
        cout.write(p, strlen(p))<<endl;
        cout.write(p, strlen(p) - 4);
        //cout.write(p, strlen(p) + 4);
        return 0;
}

int main05(int argc, char const *argv[])
{
        //使用类成员函数
        cout<<"<start>";
        cout.width(30);
        cout.fill('*');
        cout.setf(ios::showbase);
        cout.setf(ios::internal);
        cout<<hex<<123<<"<End>\n";

        //使用控制符
        cout<<endl<<endl;
        cout<<"<Start>"
                <<setw(30)
                <<setfill('*')
                <<setiosflags(ios::showbase)
                <<setiosflags(ios::internal)
                <<hex
                <<123
                <<"<End>\n";
        return 0;
}

//文件操作默认是对ASSII方式进行读写
int main06(int argc, char const *argv[])
{
        char * fname = "./2aaa.txt";
        ofstream fout(fname, ios::app); //建一个 输出流对象 和文件关联
        if(!fout){
                cout<<"打开文件失败"<<endl;
                return -1;
        }
        fout<<"hello 111"<<endl;
        fout<<"hello 222"<<endl;
        fout<<"hello 333"<<endl;
        fout.close();

        //读文件
        ifstream fin(fname, ios::in);//建一个 输入流对象 和文件关联
        char ch;
        while (fin.get(ch))
        {
                cout<<ch;
        }
        fin.close();
        
        return 0;
}


//以二进制方式操作
class Teacher{
        public:
                Teacher(){
                        age = 33;
                        strcpy(name, "");
                }
                Teacher(int _age, char *_name){
                        age  = _age;
                        strcpy(name, _name);
                }
                void printT(){
                        cout<<"name:"<<name<<" age:"<<age<<endl;
                }
        private:
                int age;
                char name[32];
};
//这样就实现对象的序列化
int main(int argc, char const *argv[])
{
        char *fname = "./1.txt";
        ofstream fout(fname, ios::binary);
        if(!fout){
                cerr<<"打开文件失败"<<endl;
                return -1;
        }
        Teacher t1(31, "t31");
        Teacher t2(31, "t32");
        fout.write((char *)&t1, sizeof(t1));
        fout.write((char *)&t2, sizeof(t2));
        fout.close();

        cout<<endl;

        ifstream fin(fname);
        Teacher tmp;
        fin.read((char *)&tmp, sizeof(Teacher));
        tmp.printT();
        fin.read((char *)&tmp, sizeof(Teacher));
        tmp.printT();
        fin.close();
        return 0;
}
